package cn.zj.logistics.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.zj.logistics.mo.MessageObject;
import cn.zj.logistics.pojo.BaseData;
import cn.zj.logistics.pojo.BaseDataExample;
import cn.zj.logistics.pojo.BaseDataExample.Criteria;
import cn.zj.logistics.service.BaseDataService;

@Controller
@RequestMapping("/baseData")
public class BaseDataController {

	@Autowired
	private BaseDataService baseDataService;
	
	@Autowired
	private BaseDataService userService;

	@RequestMapping("/baseDataPage")
	public String baseDataPage() {

		return "baseDataPage";
	}

	@RequestMapping("/list")
	@ResponseBody
	public PageInfo<BaseData> list(@RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "10") Integer pageSize, String keyword) {

		PageHelper.startPage(pageNum, pageSize);

		// 条件查询对象
		BaseDataExample example = new BaseDataExample();

		if (StringUtils.isNotBlank(keyword)) {
			// 创建条件限制对象
			Criteria criteria = example.createCriteria();
			criteria.andBaseNameLike("%" + keyword + "%");
		}

		List<BaseData> baseDatas = baseDataService.selectByExample(example);
		for (BaseData baseData : baseDatas) {
			System.out.println(baseData);
		}

		PageInfo<BaseData> pageInfo = new PageInfo<BaseData>(baseDatas);

		return pageInfo;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public MessageObject delete(Long baseId) {
		
		MessageObject mo = new MessageObject(0, "删除数据失败，请联系管理员");
		//删除基础数据之前先根据要被删除的基础数据去查询是否还拥有子基础数据，如果有，不能删除
		
		BaseDataExample example = new BaseDataExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(baseId);
		
		List<BaseData> users = userService.selectByExample(example );
		
		if(users.size() > 0) {
			mo = new MessageObject(0, "此分类数据还有子数据，不能删除");
		}else {
			// 执行删除操作
			int row = baseDataService.deleteByPrimaryKey(baseId);
			if (row == 1) {
				mo = new MessageObject(1, "删除数据成功");
			}
		}
		return mo;
	}

	// 编辑功能
	@RequestMapping("/edit")
	public String edit(Model m, Long baseId) {

		// 根据id查询出BaseData对象，以供修改的回显
		if (baseId != null) {
			BaseData baseData = baseDataService.selectByPrimaryKey(baseId);
			m.addAttribute("baseData", baseData);
		}
		
		//查询出所有分类数据，以供选择
		//只查询parent_id 为 null （分类数据） 
		BaseDataExample example = new BaseDataExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdIsNull();
		
		List<BaseData> parents = baseDataService.selectByExample(example );
		m.addAttribute("parents", parents);

		return "baseDataEdit";
	}

	// 检查基础数据是否存在
	@RequestMapping("/checkBaseName")
	@ResponseBody
	public boolean checkBaseName(String baseName) {

		BaseDataExample example = new BaseDataExample();
		Criteria criteria = example.createCriteria();
		criteria.andBaseNameEqualTo(baseName);

		List<BaseData> baseDatas = baseDataService.selectByExample(example);
		return baseDatas.size() > 0 ? false : true;
	}

	// 新增操作
	@RequestMapping("/insert")
	@ResponseBody
	public MessageObject insert(BaseData baseData) {

		MessageObject mo = new MessageObject(0, "新增数据失败，请联系管理员");

		// 执行新增操作
		int row = baseDataService.insert(baseData);
		if (row == 1) {
			mo = new MessageObject(1, "新增数据成功");
		}

		return mo;
	}

	// 修改操作
	@RequestMapping("/update")
	@ResponseBody
	public MessageObject update(BaseData baseData) {

		MessageObject mo = new MessageObject(0, "修改数据失败，请联系管理员");

		// 执行修改操作
		int row = baseDataService.updateByPrimaryKeySelective(baseData);
		if (row == 1) {
			mo = new MessageObject(1, "修改数据成功");
		}
		return mo;
	}
}
